﻿using AdapterPattern.Sample;
using System;
using System.Configuration;
using System.Reflection;

namespace AdapterPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello AdapterPattern!");

            {
                // 类适配器
                ClassAdapter classAdapter = new ClassAdapter();
                string str1 = classAdapter.Request();
                //Console.WriteLine(str1);

                // 对象适配器
                Adaptee adaptee = new Adaptee();
                ObjectAdapter objectAdapter = new ObjectAdapter(adaptee);
                string str2 = objectAdapter.Request();
                //Console.WriteLine(str2);
            }

            {
                // 读取App.config 配置文件
                string adapterType = ConfigurationManager.AppSettings["adapter"];
                // 反射生成对象
                ScoreOperation operation = (ScoreOperation)Assembly.Load("AdapterPattern").CreateInstance(adapterType);
                int[] scores = { 66,49,82,67,91,78,98,59,88 }; // 定义成绩数组
                Console.WriteLine("排序前成绩结果：");
                foreach (var item in scores)
                {
                    Console.Write($"{item},");
                }

                Console.WriteLine("\n排序后成绩结果：");
                int[] reslut = operation.Sort(scores);
                foreach (var item in reslut)
                {
                    Console.Write($"{item},");
                }

                Console.WriteLine("\n【目标】查找成绩：88");
                int score = operation.Search(reslut, 88);
                if (score != -1) Console.WriteLine("【结果】找到成绩：88");
                else Console.WriteLine("【结果】没有找到成绩：88");

                Console.WriteLine("【目标】查找成绩：100");
                score = operation.Search(reslut, 100);
                if (score != -1) Console.WriteLine("【结果】找到成绩：100");
                else Console.WriteLine("【结果】没有找到成绩：100");
            }

            Console.ReadKey();
        }
    }

    #region 类适配器
    interface ITarget
    {
        string Request();
    }

    class Adaptee
    {
        public string SpecificRequest() => "Adaptee.SpecificRequest";
    }

    class ClassAdapter : Adaptee, ITarget
    {
        public string Request() => $"ClassAdapter.Request 调用 {base.SpecificRequest()}";
    }
    #endregion

    #region 对象适配器
    class ObjectAdapter : ITarget
    {
        private readonly Adaptee _Adaptee; //维持一个适配者对象的引用

        public ObjectAdapter(Adaptee adaptee)
        {
            _Adaptee = adaptee;
        }

        public string Request() => $"ObjectAdapter.Request 调用 {_Adaptee.SpecificRequest()}";
    }
    #endregion

    #region 省缺适配器(单接口适配器)模式
    interface IServiceInterface 
    {
        void ServiceMethod1();
        void ServiceMethod2();
        void ServiceMethod3();
    }

    abstract class AbstractServiceClass : IServiceInterface
    {
        public void ServiceMethod1() { } // 空方法

        public void ServiceMethod2() { }

        public void ServiceMethod3() { }
    }
    #endregion

    #region 双向适配器模式
    class TwoWayApapter : Adaptee, ITarget
    {
        // 同时维持抽象目标类和适配者的引用
        private readonly Adaptee _Adaptee;
        private readonly ITarget _Target;

        public TwoWayApapter(Adaptee adaptee) 
        {
            _Adaptee = adaptee;
        }

        public TwoWayApapter(ITarget target)
        {
            _Target = target;
        }

        public new string SpecificRequest() => _Target.Request();

        public string Request() => _Adaptee.SpecificRequest();
    }
    #endregion

}
